package com.example.controller;

import com.example.model.ThirdAppModel;
import com.example.repository.AuthorRepository;
import com.example.service.UserService;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import javax.validation.constraints.NotNull;

/**
 * @author eric
 * @program demo
 * @description
 * @date 2022/1/16
 */
@Controller
public class HelloWorldController {

    private static final Logger log = LoggerFactory.getLogger(HelloWorldController.class);


    @Inject
    UserService userService;

    @Inject
    private AuthorRepository repository;

    @Get(uris = {"/index", "/hello"})
    public String index() {
        return "Hello World";
    }

    @Get(uris = {"/login", "/api/login"})
    public String login(@NotNull(message = "userName不能为空") String userName) {
        return userService.login();
    }

    @Get("/all")
    Flux<ThirdAppModel> all() {
        return repository.findAll();
    }


    @Get("/id")
    Mono<ThirdAppModel> get(Long id) {
        return repository.findById(id);
    }

    /**
     * @return https://blog.51cto.com/liukang/2090198
     */
    @Get("/save")
    Mono<ThirdAppModel> save() {
        ThirdAppModel thirdAppModel = new ThirdAppModel();
        thirdAppModel.setId(1L);
        thirdAppModel.setAppSecret("111");

        return Mono.just(thirdAppModel);
//
//        repository.save(thirdAppModel)
//                .onErrorResume(e ->     // 1
//                        userRepository.findByUsername(user.getUsername())   // 2
//                                .flatMap(originalUser -> {      // 4
//                                    user.setId(originalUser.getId());
//                                    return userRepository.save(user);   // 3
//                                }));
//
//
//        return repository.save(thirdAppModel);
    }
}
